package com.smartsofa.util;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.smartsofa.R;

public class CusorProgressBar extends View {
	private Context mContext;
	private float mMax=100;
	private float mMin=0;
	private float mProgress;
	private float mProgressb;
	private int mBarColor;
	private int mTextColor;
	private float mTextSize;
	private int mBarLength;
	private int mStyleMode;//0 is for xueya,1 is for xuetang,2 is for xueyang 
	

	private final int DEFAULT_BAR_COLOR=Color.BLUE;
	private final int DEFAULT_TEXT_COLOR=Color.DKGRAY;
	private final int DEFAULT_STYLE_MODE=0;
	private final float DEFAULT_TEXT_SIZE=sp2px(10);
	
	
	private Paint mLinePaint;
	private Paint mTextPaint;
	private Paint mBitPaint;
	private Paint mTextPaint2;
	private Paint mDotPaint;
	private Bitmap mCursor;
	
	private int spaceWidth = MyUtils.dip2px(getContext(), 4);
	private int textWidth = MyUtils.sp2px(getContext(), 16);
	private int textWidth2 = MyUtils.sp2px(getContext(), 10);
	private int lineWidth = MyUtils.dip2px(getContext(),2);
	private int cursorWidth = MyUtils.dip2px(getContext(), 4);
	private int dotwidth = MyUtils.dip2px(getContext(), 4);

	public CusorProgressBar(Context context) {
		this(context, null);
	}

	public CusorProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, R.attr.cusorProgressBarStyle);
	}
	public CusorProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext=context;
		
		init();
		final TypedArray attr=getContext().
				getTheme().
			obtainStyledAttributes(attrs, R.styleable.CusorProgressBar, defStyleAttr, 0);
		try {
			mStyleMode=attr.getInt(R.styleable.CusorProgressBar_stylemode, 
					DEFAULT_STYLE_MODE);
			mTextSize = attr.getDimension(
					R.styleable.CusorProgressBar_cursor_text_size,
					DEFAULT_TEXT_SIZE);
			mTextColor = attr.getColor(R.styleable.CusorProgressBar_cursor_text_color, 
					DEFAULT_TEXT_COLOR);
		} finally {
			attr.recycle();
			}
		
		
	}


	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
	    int width=getWidth();
	    int height=getHeight();
	    Log.i("width",""+width);
	    
	    int bWidth=mCursor.getWidth();
	    int bHeight=mCursor.getHeight();
	    float delta;
	    canvas.drawLine(0.0f, height-spaceWidth*2-textWidth-cursorWidth-lineWidth, 
	    		width,height-spaceWidth*2-textWidth-cursorWidth-lineWidth, mLinePaint);
	    canvas.drawCircle(dotwidth, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,dotwidth, mDotPaint);
	    canvas.drawCircle(width-dotwidth, height-spaceWidth*2-textWidth-cursorWidth-lineWidth, dotwidth, mDotPaint);

	    switch (mStyleMode){
	    case 0:
	    	mMax = 180;
	    	mMin = 30;
	    	delta = mMax-mMin;
	    	canvas.drawLine((60-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
	    			(60-mMin)/delta*width, height-spaceWidth*2-textWidth, mLinePaint);
	    	canvas.drawLine((90-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
	    			(90-mMin)/delta*width, height-spaceWidth*2-textWidth, mLinePaint);
	    	canvas.drawLine((140-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
	    			(140-mMin)/delta*width, height-spaceWidth*2-textWidth, mLinePaint);
	    	System.out.println("mProgress----"+mProgress);
	    	
    		canvas.drawBitmap(mCursor,(mProgress-mMin)/delta*width-bWidth*0.5f
	    			,height-bHeight-spaceWidth*2-textWidth-cursorWidth-lineWidth, mBitPaint);
	    	canvas.drawBitmap(mCursor,(mProgressb-mMin)/delta*width-bWidth*0.5f
	    			,height-bHeight-spaceWidth*2-textWidth-cursorWidth-lineWidth, mBitPaint);
	    	
	    	canvas.drawText(" "+mProgress,(mProgress-mMin)/delta*width-bWidth*0.5f,
	    			height-bHeight-spaceWidth*3-textWidth-cursorWidth-lineWidth, mTextPaint);
	    	canvas.drawText(" "+mProgressb,(mProgressb-mMin)/delta*width-bWidth*0.5f,
	    			height-bHeight-spaceWidth*3-textWidth-cursorWidth-lineWidth, mTextPaint);
       	
	    	
	    	canvas.drawText(""+60,(60-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	canvas.drawText(""+90,(90-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	canvas.drawText(""+140,(140-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	break;
	    case 1:
	    	mMax=12;
	    	mMin=3.5f;
	    	delta = mMax-mMin;
	    	
	    	canvas.drawLine((3.9f-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
	    			(3.9f-mMin)/delta*width, height-spaceWidth*2-textWidth-lineWidth, mLinePaint);
	    	canvas.drawLine((11.1f-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
			(11.1f-mMin)/delta*width, height-spaceWidth*2-textWidth-lineWidth, mLinePaint);
	    	canvas.drawBitmap(mCursor,(mProgress-mMin)/delta*width-bWidth*0.5f
	    			,height-bHeight-spaceWidth*2-textWidth-cursorWidth-lineWidth, mBitPaint);
	    	canvas.drawText("  "+mProgress,(mProgress-mMin)/delta*width-bWidth*0.5f,
	    			height-bHeight-spaceWidth*3-textWidth-cursorWidth-lineWidth, mTextPaint);

	    	canvas.drawText(""+3.9,(3.9f-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	canvas.drawText(""+11.1,(11.1f-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	break;
	    case 2:
	    	mMax=99;
	    	mMin=94;
	    	delta=mMax-mMin;
	    	canvas.drawLine((98-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
	    			(98-mMin)/delta*width, height-spaceWidth*2-textWidth-lineWidth, mLinePaint);
	    	canvas.drawLine((95-mMin)/delta*width, height-spaceWidth*2-textWidth-cursorWidth-lineWidth,
	    			(95-mMin)/delta*width, height-spaceWidth*2-textWidth-lineWidth, mLinePaint);
	    	canvas.drawBitmap(mCursor,(mProgress-mMin)/delta*width-bWidth*0.5f
	    			,height-bHeight-spaceWidth*2-textWidth-cursorWidth-lineWidth, mBitPaint);
	    	canvas.drawText("  "+mProgress,(mProgress-mMin)/delta*width-bWidth*0.5f,
	    			height-bHeight-spaceWidth*3-textWidth-cursorWidth-lineWidth, mTextPaint);

	    	canvas.drawText(""+95,(95f-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	canvas.drawText(""+98,(98f-mMin)/delta*width, height-spaceWidth-textWidth*0.5f, mTextPaint2);
	    	
	    	break;
	    default :break;
	    }
	
	}
	

	private void init() {
		int linecolor = getContext().getResources().getColor(R.color.lightbule);
		int textcolor = getContext().getResources().getColor(R.color.darkbule);
		int textcolor2 = getContext().getResources().getColor(R.color.darkgray);
		mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mLinePaint.setColor(linecolor);
		mLinePaint.setStrokeWidth(lineWidth);
		
		Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
		mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mTextPaint.setTextAlign(Paint.Align.CENTER);
		mTextPaint.setColor(textcolor);
		mTextPaint.setTextSize(textWidth);
		mTextPaint.setTypeface(typeface);
		mBitPaint = new Paint();
		mCursor=BitmapFactory.decodeResource(getResources(),R.drawable.cursor);
		
		mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mDotPaint.setColor(linecolor);
		mDotPaint.setStrokeWidth(10.0f);
		
		typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
		mTextPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
		mTextPaint2.setTextAlign(Paint.Align.CENTER);
		mTextPaint2.setColor(textcolor2);
		mTextPaint2.setTextSize(textWidth2);
		mTextPaint2.setTypeface(typeface);
	}

	public float sp2px(float sp) {
		final float scale = getResources().getDisplayMetrics().scaledDensity;
		return sp * scale;
	}

	
	public float getProgress() {
		return mProgress;
	}
	public void setProgress( float progress){
		
			this.mProgress=progress;
			invalidate();
		
	}
	public float getProgressb() {
		return mProgressb;
	}
	public void setProgressb( float progress){
		
			this.mProgressb=progress;
			invalidate();
		}
	

}
